const express = require('express');
const userController = require('../controllers/userController');
const authMiddleware = require('../../../middleware/authMiddleware');
const router = express.Router();
const multer = require('multer');
const path = require('path');
const productController = require('./../controllers/productController');

// 配置 Multer
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads/'); // 存储位置
    },
    filename: (req, file, cb) => {
        const ext = path.extname(file.originalname);
        cb(null, Date.now() + ext); // 文件名
    }
});

const upload = multer({ storage: storage });

// 获取用户信息接口
router.get('/user/info', authMiddleware, userController.getUser);
router.post('/login', userController.login);
router.get('/user/allusers', userController.getAllUsersController);
router.post('/user/adduser', userController.addUser);

// 图片上传接口
router.post('/upload', upload.single('image'), (req, res) => {
    if (!req.file) {
        return res.status(400).json({ message: '请上传图片' });
    }
    const imageUrl = `/uploads/${req.file.filename}`; // 图片URL
    res.status(200).json({ message: '上传成功', imageUrl: imageUrl });
});

// 前台产品列表接口
router.get('/product/searchProduct', productController.searchProduct);
router.get('/product/productDetail/:id', productController.getProductDetail);
module.exports = router;